package one.stand.controller;

import com.alibaba.fastjson.JSON;
import com.jmp.api.admin.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.UserAdminListDto;
import com.jmp.base.dto.UserAdminListVo;
import com.jmp.base.dto.UserAdminSaveDto;
import com.jmp.base.dto.UserAdminSaveVo;
import com.jmp.feign.UserFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.advice.ResultEnum;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.MenuMapperExt;
import one.stand.model.MenuModel;
import one.stand.model.ResultModel;
import one.stand.service.user.MenuService;
import one.stand.service.user.UserAdminService;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cjr
 */
@RestController
@RequestMapping("adminApi/company")
@Api(description = "后台用户管理")
@Slf4j
public class AdminController {
    @Autowired
    private UserFeign userFeign;

    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuMapperExt menuMapperExt;

    @ApiOperation(value = "查询总后台管理员列表")
    @PostMapping("selectAdminList")
    @SecurityAuthCheck
    public ResultModel<AdminListResponse> selectAdminList(AdminListRequest request) {
        UserAdminListDto userAdminListDto = UserAdminListDto.builder()
                .keyword(request.getLoginName())
                .build();
        userAdminListDto.setPageInit(request.getPageNum(), request.getPageSize());
        ResultModel<UserAdminListVo> resultModel = userFeign.adminList(userAdminListDto);
        AssertUtil.isTrue(resultModel);

        PageResponse2<AdminListResponse.Admin> modelList = ConvertUtils.page2(resultModel.getData().getPage(), model -> {
            // 加载菜单列
            List<MenuModel> menuModelList = menuMapperExt.select(new MenuModel() {{
                setAdminId(model.getAdminId());
            }});
            return AdminListResponse.Admin.builder()
                    .adminId(model.getAdminId())
                    .loginName(model.getLoginName())
                    .name(model.getName())
                    .sex(model.getSex())
                    .position(model.getPosition())
                    .tourist(model.getTourist())
                    .isDel(model.getIsDel())
                    .modelList(menuModelList)
                    .build();
        });
        AdminListResponse response = new AdminListResponse();
        response.setPageInfo(modelList);
        return ResultModel.success(response);
    }

    @ApiOperation(value = "新增总/修改后台管理员")
    @PostMapping("saveAdmin")
    @SecurityAuthCheck
    public ResultModel<Integer> saveAdmin(@Valid AdminSaveRequest request) {
        AssertUtil.nonNull(request.getLoginName(), ResultEnum.PARAM_CHECK, "请输入管理员账号~！");
        ResultModel<UserAdminSaveVo> resultModel = userFeign.adminSave(UserAdminSaveDto.builder()
                .adminId(request.getAdminId())
                .loginName(request.getLoginName())
                .passWord(request.getPassWord())
                .name(request.getName())
                .sex(request.getSex())
                .position(request.getPosition())
                .build());
        AssertUtil.isTrue(resultModel);
        return ResultModel.success(resultModel.getData().getId());
    }

    @ApiOperation(value = "启用/禁用")
    @PostMapping("delAdmin")
    @SecurityAuthCheck
    public ResultModel<Boolean> delAdmin(@Valid AdminDelRequest request) {
        log.info("启用/禁用管理员：{}", JSON.toJSONString(request));
        if (request == null || request.getAdminId() == null || request.getIsDel() == null) {
            return ResultModel.checkParamFail();
        }
        ResultModel<UserAdminSaveVo> resultModel = userFeign.adminSave(UserAdminSaveDto.builder()
                .adminId(request.getAdminId())
                .isDel(request.getIsDel())
                .build());
        AssertUtil.isTrue(resultModel);
        return ResultModel.success(true);
    }

    @ApiOperation(value = "查询管理员权限列表")
    @PostMapping("selectMenuList")
    @SecurityAuthCheck
    public ResultModel<List<MenuModel>> selectMenuList() {
        List<MenuModel> list = menuMapperExt.selectAllList(new MenuModel() {{
        }});
        return ResultModel.success(list);
    }

    @ApiOperation(value = "设置总后台管理员权限")
    @PostMapping("inserAdminRole")
    @SecurityAuthCheck
    public ResultModel<Boolean> inserAdminRole(AdminRoleRequest request) {
        if (request == null || request.getAdminId() == null || request.getRoleIds() == null) {
            return ResultModel.checkParamFail();
        }
        menuService.saveAdminRole(request.getAdminId(), request.getRoleIds());

        return ResultModel.success(true);
    }
}
